#-------------------------------------------------------------------------------
# Name:        ObjectHandler
# Purpose:
#
# Author:      Pablo
#
# Created:     17/09/2013
# Copyright:   (c) Monxcleyr Productions 2013
# Licence:     GPL v3
#-------------------------------------------------------------------------------
#!/usr/bin/env python

import glob, os, logging
import objects
import config

class ObjectHandler():

  """Contains all the objects used in game"""

  def __init__(self, imagehandler):
    self.objects = []
    self.imagehandler = imagehandler
    self.conf = config.Config()

    self.loadFromINI('resources\objects\*')
    self.rects = []


  def loadFromINI(self, inidir):
    """ Loads all the .INIs from inidir and create the objects """

    logging.info('Loading objects from %s', inidir)
    files = glob.glob(inidir)

    for filename in files:
      self.conf.read(filename)
      objtype = self.conf.get('Object', 'type')
      name = self.conf.get('Object', 'name')
      attr = dict(self.conf.items('Attributes'))
      if objtype not in objects.types:
        logging.warning('Failed to load object with invalid type "%s" with name "%s"', objtype, name)
        continue
      if name in self.getNames():
        logging.warning('Object with name "%s" already exists, creating second object with same name' % name)
      obj = objects.types[objtype](objtype, name, attr, self.imagehandler)
      self.objects.append(obj)

  def update(self, common, computer):

    """ Update any objects that have an update method. """

    self.getObjectByType('player').update(common)

    for obj in self.getObjectTypeList('animatedsprite'):
      obj.update(common)

    for obj in self.getObjectTypeList('light'):
      obj.update(common, computer)

    for obj in self.getObjectTypeList('console'):
      obj.update(common, self.getObjectByType('player'))


  def draw(self, surface, offset, common):

    self.rects = []
    objects = sorted(self.objects, key=lambda obj: obj.attributes['priority'], reverse=True)
    for obj in objects:
      self.rects.append(obj.draw(surface, offset, common))

##    print rects


  def getNames(self):

    """ Returns the names of all the objects currently in the handler"""
    temp = []
    for obj in self.objects:
      temp.append(obj.name)
    return temp

  def getObjectByType(self, objtype):

    """ Returns first object found with given type. Useful only when there is only one of this type, eg. player. """
    for obj in self.objects:
      if obj.type == objtype:
        return obj
    return None

  def getObjectTypeList(self, objtype):

    """ Returns a list of objects fo the given type. """

    temp = []
    for obj in self.objects:
      if obj.type == objtype:
        temp.append(obj)

    return temp

  def getObjectAncestorList(self, objtype):

    """ Returns a list of objects with the given ancestor, will include the given object type, too """

    temp = []
    for obj in self.objects:
      if objtype in obj.ancestors:
        temp.append(obj)

    return temp

  def getObject(self, name):
    for obj in self.objects:
      if name == obj.name:
        return obj
    return None

